/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.analysis;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

import edu.strath.cis.grallan.findbugs.adaptiveranking.population.AlertPopulationMap;
import edu.strath.cis.grallan.findbugs.adaptiveranking.population.Population;
import edu.strath.cis.grallan.findbugs.adaptiveranking.population.PopulationExtractor;
import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.gui2.BugLoader;

/**
 * 
 * Quickly thrown together class to print the sizes of each population.
 * 
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class PopulationSizePrinter {

	public static void main(String[] args) throws IOException {

		String baseDir = "/home/graham/uni/FinalYearProject/fb-projects/fll-sw/";
		File projectFile = new File(baseDir + "fll-sw-unclassified.xml");
		Project loadProject = BugLoader.loadProject(null, projectFile);
		BugCollection bugCollection = BugLoader.loadBugs(null, loadProject, projectFile);

		PopulationExtractor prototype = PopulationExtractor.instanceFor(bugCollection);

		// writeCSVFile(prototype);

		writePropertiesFile(prototype, baseDir);

	}

	private static void writePropertiesFile(PopulationExtractor prototype, String baseDir) throws IOException {
		for (AlertPopulationMap alertMap : prototype.alertPopulationMaps()) {

			String outputFile = baseDir + "alertMap-" + alertMap.type().name() + ".properties";

			Properties populationSizes = new Properties();

			for (Population population : alertMap.allPopulations()) {
				int size = population.size();
				populationSizes.put(population.populationDescriptor(), String.valueOf(size));
			}

			populationSizes.store(new FileWriter(outputFile), "Population sizes [" + alertMap.type().name() + "]");
		}
	}

	// @SuppressWarnings("unused")
	// private static void writeCSVFile(PopulationExtractor prototype) throws IOException {
	// int i = 0;
	// for (AlertPopulationMap alertMap : prototype.alertPopulationMaps()) {
	//
	// String outputFile = "/home/graham/uni/FinalYearProject/evaluation/ciboodle/alertMap-" +
	// alertMap.type() + ".csv";
	//
	// FileWriter writer = new FileWriter(outputFile);
	//			
	// for (Population population : alertMap.allPopulations()) {
	// int size = population.size();
	// writer.write(size + ",");
	// }
	//
	// writer.close();
	// i++;
	// }
	// }

}
